package com.yingxin.yxerp.module.message.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yingxin.yx.framework.commons.dto.PageOut;
import com.yingxin.yx.framework.commons.enums.BusinessType;
import com.yingxin.yx.framework.mybatisplus.PageHelper;
import com.yingxin.yx.framework.user.core.IUserSupport;
import com.yingxin.yxerp.module.common.ValueLabel;
import com.yingxin.yxerp.module.message.constant.UserMsgType;
import com.yingxin.yxerp.module.message.mapper.UserMessageMapper;
import com.yingxin.yxerp.module.message.service.IUserMessageService;
import com.yingxin.yxerp.module.message.vo.req.UserMessageQueryVo;
import com.yingxin.yxerp.module.message.vo.req.UserMessageReadBatchReqVo;
import com.yingxin.yxerp.module.message.vo.resp.UserMessageRespVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 用户消息 前端控制器
 * @author fanchao
 * @since 2022-03-21
 */

@RestController
@RequestMapping("/user/message")
public class UserMessageController {

    @Autowired
    private IUserMessageService userMessageService;

    @SuppressWarnings("all")
    @Autowired
    private UserMessageMapper userMessageMapper;

    @Autowired
    private IUserSupport userSupport;

    @ApiOperation(value = "单据类型")
    @GetMapping("/business-type")
    public List<ValueLabel> listBusinessType() {
        List<ValueLabel> list = new ArrayList<>();
        for (BusinessType i : BusinessType.values()) {
            ValueLabel value = new ValueLabel();
            value.setValue(i.getType());
            value.setLabel(i.getTypeName());
            list.add(value);
        }
        return list;
    }

    @ApiOperation(value = "消息类型")
    @GetMapping("/msg-type")
    public List<ValueLabel> listMsgType() {
        List<ValueLabel> list = new ArrayList<>();
        for (UserMsgType i : UserMsgType.values()) {
            ValueLabel value = new ValueLabel();
            value.setValue(i.getType());
            value.setLabel(i.getTypeName());
            list.add(value);
        }
        return list;
    }

    @ApiOperation(value = "查询列表（分页）")
    @PostMapping("/page")
    public PageOut<UserMessageRespVo> page(@RequestBody UserMessageQueryVo queryVo) {
        queryVo.setReceiverId(userSupport.getUserId());
        IPage<UserMessageRespVo> page = userMessageMapper.pageVo(PageHelper.in(queryVo), queryVo);
        return PageHelper.out(page);
    }

    @ApiOperation(value = "根据id查询")
    @GetMapping("/{id}")
    public UserMessageRespVo get(@PathVariable("id") String id) {
        UserMessageRespVo vo = userMessageMapper.getVo( id );
        if( ObjectUtil.isNotEmpty( vo ) ){
            userMessageService.read( id );
        }
        return vo;
    }

    @ApiOperation(value = "批量设置已读")
    @PostMapping("/readBatch")
    public void readBatch(@RequestBody UserMessageReadBatchReqVo queryVo) {
        userMessageService.readBatch( queryVo );
    }

    @ApiOperation(value = "全部已读")
    @GetMapping("/all/read")
    public void allRead() {
        userMessageService.allRead();
    }

}
